<?php declare(strict_types=1);
/**
 * Created by IntelliJ IDEA.
 * User: 张伯发
 * Date: 2019/7/29
 * Time: 16:41
 */

namespace App\Http\Controller\Happy;


use App\Exception\ApiException;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\Middleware;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
use Swoft\Log\Helper\Log;
use App\Http\Middleware\ControllerMiddleware;

/**
 * @Controller("happy/test")
 * @Middleware( ControllerMiddleware::class )
 */
class TestController
{
    /**
     * @RequestMapping("log")
     * @return array
     * @throws \ReflectionException
     * @throws \Swoft\Bean\Exception\ContainerException
     * @author 张伯发 2019/7/29 16:50
     */
    public function log(): array
    {
        // Tag start
        Log::profileStart('tagName');

        Log::debug('this %s log', 'debug');
        Log::info('this %s log', 'info');
        Log::warning('this %s log', 'warning');
        Log::error('this %s log', 'error');
        Log::alert('this %s log', 'alert');
        Log::emergency('this %s log', 'emergency');

        Log::pushLog('key', 'value');
        Log::pushLog('key', ['value']);
        Log::pushLog('key', 'value');

        Log::profileEnd('tagName');

        Log::counting('mget', 1, 10);
        Log::counting('mget', 2, 10);


        return ["log test"];
    }


    /**
     * @param string $name
     * @return array
     * @author 张伯发 2019/7/29 17:47
     *
     * @RequestMapping(route="say/{name}", method={RequestMethod::GET})
     */
    public function say(string $name = ''): array
    {
        return ["message" => $name];
    }

    /**
     * @RequestMapping("error")
     * @throws ApiException
     * @author 张伯发 2019/7/30 11:36
     */
    public function error()
    {
        throw new ApiException('this is error api');
    }

}
